<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>奖励管理</el-breadcrumb-item>
      <el-breadcrumb-item>奖励列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 任务列表 -->

      <el-table :data="shopList" style="width: 100%" border stripe>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="名字">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="奖励介绍">
                <span>{{ props.row.introduce }}</span>
              </el-form-item>
              <el-form-item label="兑换需求等级">
                <span>{{ props.row.prepositionLevel }}</span>
              </el-form-item>
              <el-form-item label="兑换所需金币">
                <span>{{ props.row.money}}</span>
              </el-form-item>
              <el-form-item label="可兑换次数">
                <span>{{ props.row.amount }}</span>
              </el-form-item>
              <el-form-item label="兑现限时">
                <span>{{ props.row.timeout }} (小时)</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="奖励名称" prop="name"></el-table-column>
        <el-table-column label="兑换所需金币" prop="money"></el-table-column>
        <el-table-column label="兑换需求等级" prop="prepositionLevel"></el-table-column>
        <el-table-column label="剩余兑换次数" prop="amount"></el-table-column>
        <el-table-column label="兑换">
          <template slot-scope="pro">
            <el-button type="primary" @click="conversionDialog(pro.row.id)">兑换</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog
      title="兑换数量"
      :visible.sync="conversionVisible"
      width="40%"
      @close="buyDialogClosed"
      :close-on-click-modal="false"
      destroy-on-close="true"
    >
      <template>
        <el-form ref="buyTaskFormRef" :model="conversion" label-width="80px">
          <el-form-item prop="conversion">
            <el-slider v-model="conversion.amount" show-input></el-slider>
          </el-form-item>
          <el-form-item class="buyButton">
            <el-button @click="conversionVisible = false; this.conversion = {}">取 消</el-button>
            <el-button type="primary" @click="buy">确 定</el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped>
.buyButton {
  margin-left: 360px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import {secoondaryBuyShop, secoondaryGetShopList} from '@/utils/SecondaryApi'
export default {
  data() {
    return {
      conversion: {
        shopId: "",
        amount: 1,
      },
      conversionVisible: false,
      queryInfo: {
        query: "",
        // 当前页数
        pageNum: 1,
        // 每页显示多少数据
        pageSize: 5,
      },

      addShopInfo: {
        id: "",
        name: "",
        introduce: "",
        prepositionLevel: "",
        money: "",
        amount: "",
        url: "",
      },
      shopList: [],
      total: 0,
    };
  },
  created() {
    this.getShopList();
  },
  methods: {
    buyDialogClosed() {
      this.$refs.buyTaskFormRef.resetFields();
    },
    async buy() {
      const { data: res } = await secoondaryBuyShop(this.conversion);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.message);
        this.conversionVisible = false;
        return;
      }
      this.conversionVisible = false;
      this.$message.success("兑换成功！");
      this.$store.commit("getUserInfo");
      this.conversion = {};
      this.getShopList();
    },
    conversionDialog(id) {
      this.conversionVisible = true;
      this.conversion.shopId = id;
    },
    async getShopList() {
      console.log("dasd");
      const { data: res } = await secoondaryGetShopList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取奖励列表失败！");
      }
      this.shopList = res.data.list;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getShopList();
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageNum = newSize;
      this.getShopList();
    },
  },
};
</script>